package org.wyn.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.wyn.mongodb.bean.UserDetail;
import org.wyn.mongodb.bean.UserStatus;
import org.wyn.mongodb.dao.impl.FreshThingDao;
import org.wyn.mongodb.dao.impl.FriendRelationshipDao;
import org.wyn.mongodb.dao.impl.UserDetailDao;
import org.wyn.mongodb.dao.impl.UserStatusDao;
import org.wyn.service.IStatusService;

import com.google.inject.Inject;
/**
 * "状态"service实现
 * @author wyn
 *
 */
public class StatusServiceImpl implements IStatusService{
	@Override
	public boolean publishStatus(String userName, String content) {
		if(StringUtils.isBlank(content)){//状态内容不能为空
			return false;
		}
		UserDetail ud = userDetailDao.findOne("userName",userName);
		//保存"状态"到数据库
		userStatusDao.publishStatus(userName, ud.getRealName(), ud.getPortrait(), content);
		//保存对应新鲜事
		freshThingDao.saveThingPublishStatus(userName, content);
		//更新UserDetail集合status字段
		ud.setStatus(content);
		ud.setStatusCreateTime(System.currentTimeMillis());
		userDetailDao.save(ud);
		userDetailDao.ensureIndexes();
		return true;
	}
	@Override
	public List<UserStatus> getFriendStatus(String userName) {
		List<String> friendList = friendRelationshipDao.findRelationshipByUser(userName);
		return userStatusDao.createQuery().filter("userName in", friendList).order("-createTime").asList();
	}

	@Inject
	private UserDetailDao userDetailDao;
	@Inject
	private UserStatusDao userStatusDao;
	@Inject
	private FreshThingDao freshThingDao;
	@Inject
	private FriendRelationshipDao friendRelationshipDao;
	public FreshThingDao getFreshThingDao() {
		return freshThingDao;
	}
	public void setFreshThingDao(FreshThingDao freshThingDao) {
		this.freshThingDao = freshThingDao;
	}
	public UserDetailDao getUserDetailDao() {
		return userDetailDao;
	}
	public void setUserDetailDao(UserDetailDao userDetailDao) {
		this.userDetailDao = userDetailDao;
	}
	public UserStatusDao getUserStatusDao() {
		return userStatusDao;
	}
	public void setUserStatusDao(UserStatusDao userStatusDao) {
		this.userStatusDao = userStatusDao;
	}
	public FriendRelationshipDao getFriendRelationshipDao() {
		return friendRelationshipDao;
	}
	public void setFriendRelationshipDao(FriendRelationshipDao friendRelationshipDao) {
		this.friendRelationshipDao = friendRelationshipDao;
	}
}
